<?php
require APPPATH.'libraries/ApiController.php';
class charge extends ApiController {
	public function __construct()
	{
		parent::__construct();
	}
	
	
	public function chargermb(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"会员ID"],
			["username",true,"会员姓名"],
			["freermb",true,"赠送金额"],
			["chargermb",true,"充值金额"],
			["sumrmb",true,"充值金额"],
			["paytype",true,"支付方式"],
			["memo",false,"备注信息",""]],'post');
			
			$this->db->where("uid",$data["uid"]);
			$user=$this->db->select("money")->get("user")->row_array();
			
			$oldrmb=(float)$user["money"];
			$afterrmb=$oldrmb+(float)$data["sumrmb"];
			
			$data["afterrmb"]=$afterrmb;
			$data["shopid"]=$token["shopid"];
			$data["ordercode"]='CZ'.date("Ymd").mt_rand(100000, 999999);;
			$this->db->insert("charge_rmb",$data);
	
			$this->db->where("uid",$data["uid"]);
			$this->db->update("user",array("money"=>$afterrmb));
			
			$data["paytype"]=json_decode($data["paytype"],true);
			$data["createtime"]=date("Y-m-d H:i:s");
			
			$this->WRITE("code",1);
			$this->WRITE("data",$data);
			$this->WRITE("msg","会员充值成功!");
			$this->RESPONSE();
		}
	}
	
	
	//充值次卡
	public function chargetimecard(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"会员ID"],
			["username",true,"会员姓名"],
			["sumrmb",true,"最终充值金额"],
			["timecarddata",true,"次卡信息"],
			["paytype",true,"支付方式"],
			["expdate",false,"有效期",""],
			["memo",false,"备注信息",""]],'post');
			
			
			$this->db->where("uid",$data["uid"]);
			$user=$this->db->select("times")->get("user")->row_array();
			
			$oldtimes=(float)$user["times"];
			$data["shopid"]=$token["shopid"];
			$carddata=json_decode($data["timecarddata"],true);
			$data["timecardtitle"]=$carddata["title"];
			$data["chargecount"]=(int)$carddata["count"];
			$data["count"]=(int)$carddata["count"];
			$data["ordercode"]='CC'.date("Ymd").mt_rand(100000, 999999);
			$this->db->insert("charge_timecard",$data);
			
			
		
			//计算长期有效期内的卡
			$this->db->where("shopid",$token["shopid"]);
			$this->db->where("uid",$data["uid"]);
			$this->db->select("tcrid,ordercode,uid,username,shopid,timecardtitle,expdate,chargecount,count,memo,createtime");
			$cardlist=$this->db->get("charge_timecard")->result_array();
			
			$curdate=strtotime(date('Y-m-d 00:00:00'));
			$sumcount=0;
			foreach($cardlist as $card){
				if($card["expdate"]===""){
					$sumcount+=(int)$card["count"];
				}else if($card["expdate"]!=="")
				{
					$expdate=strtotime($card["expdate"].' 23:59:59');
					if($expdate>$curdate){
						$sumcount+=(int)$card["count"];
					}else{
						var_dump("过期");
					}
				}
			}
			
			//统计支付
			$this->db->where("uid",$data["uid"]);
			$this->db->update("user",array("times"=>$sumcount));
			
			$resdata=$data;
			$resdata["paytype"]=json_decode($resdata["paytype"],true);
			unset($resdata["timecarddata"]);
			$resdata["aftertimes"]=$sumcount;
			$resdata["createtime"]=date('Y-m-d H:i:s');
			
			$this->WRITE("code",1);
			$this->WRITE("data",$resdata);
			$this->WRITE("msg","充次卡成功!");
			
			$this->RESPONSE();
		}
	}
	
	//充值记录
	public function history_rmb(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"会员ID"]],'post');
			
			$this->db->where("uid",$data["uid"]);
			$this->db->select("chargerid,uid,username,ordercode,sumrmb,memo,paytype,createtime");
			$list=$this->db->get("charge_rmb")->result_array();
			for($i=0;$i<count($list);$i++){
				$list[$i]["paytype"]=json_decode($list[$i]["paytype"],true);
			}
			
			$this->WRITE("code",1);
			$this->WRITE("msg","操作成功!");		
			$this->WRITE("list",$list);
			$this->RESPONSE();		
		}
	}
	
	public function history_timecard(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"会员ID"]],'post');
			
			$this->db->where("uid",$data["uid"]);
			$this->db->select("uid,username,timecardtitle,sumrmb,ordercode,expdate,chargecount,paytype,tcrid,createtime");
			$list=$this->db->get("charge_timecard")->result_array();
			for($i=0;$i<count($list);$i++){
				$list[$i]["paytype"]=json_decode($list[$i]["paytype"],true);
			}
			
			$this->WRITE("code",1);
			$this->WRITE("msg","操作成功!");		
			$this->WRITE("list",$list);
			$this->RESPONSE();		
		}
	}
}